//地图容器
var chart = echarts.init(document.getElementById('main'));
var industry = "A01";
var zb = -1;
//绘制全国地图
$.getJSON('js/map/map/province/yunnan.json', function(data) {
	var d = [];

	for(var i = 0; i < data.features.length; i++) {
		var name = data.features[i].properties.name
		var id = data.features[i].id
		d.push({
			name: name,
			id: id.substring(0, 4),
		})
	}

	//注册地图
	echarts.registerMap('yunnan', data);
	//绘制地图
	renderMap('yunnan', d, industry, zb);

});

//地图点击事件
chart.on('click', function(params) {
	if(params.seriesName != 'yunnan') {
		return
	}

	//显示县级地图
	$.getJSON('js/map/map/city/' + cityMap[params.name] + '.json', function(data) {
		echarts.registerMap(params.name, data);
		var d = [];
		for(var i = 0; i < data.features.length; i++) {
			console.log(cityMap[data.features[i].properties.name])
			d.push({
				name: data.features[i].properties.name,
				id: cityMap[data.features[i].properties.name]
			})
		}
		renderMap(params.name, d, industry, zb);
	});

});

//初始化绘制全国地图配置
var option = {
	backgroundColor: '#ebebeb',
	title: {
		text: '云南省企业分布',
		left: 'center',
		textStyle: {
			//color: 'gray',
			fontSize: 18,
			fontWeight: 'normal',
			fontFamily: "Microsoft YaHei"
		},
		subtextStyle: {
			//color: 'gray',
			fontSize: 14,
			fontWeight: 'normal',
			fontFamily: "Microsoft YaHei"
		}
	},
	 visualMap: {
        min: 0,
        max: 1000,
        left: 'left',
       // color: ['orangered','yellow','lightskyblue'],
        top: 'bottom',
        text:['高','低'],           // 文本，默认为数值文本
        calculable : true
    },
	//此处改悬停
	tooltip: {
		trigger: 'item',
		//formatter: '{b}'
		formatter: function (data) {
		var hyhtml = industry=='A01'?"农业":industry=='A02'?"林业":industry=='A03'?"畜牧":"渔业"
		var gmhtml = zb ==0?"50w-100w":zb ==1?"100w-500w":zb ==2?"500w-1000w":zb==3?"1000w以上":"不限"
        return "<div>"+data.data.name+"</div>"+"<div>企业类型  :  "+hyhtml+"</div>"+"<div>注册资金  :  "+gmhtml+"</div>"+"<div>合计  :  "+data.data.value+"家</div>";
    },
	},

	animationDuration: 1000,
	animationEasing: 'cubicOut',
	animationDurationUpdate: 1000

};

function renderMap(map, data, industry, zb) {

	var param = []
	for(var i = 0; i < data.length; i++) {
		param.push(JSON.stringify(data[i]))
	}

	axios({
		method: 'POST',
		url: base_url + '/map',
		params: {
			"jsonarr": param,
			"type": industry,
			"zb": zb
		},
		responseType: 'json',
	}).then(function(res) {

		var acdata = res.data.data

		option.title.subtext = map == 'yunnan' ? '云南省' : map;
		option.series = [{
			name: map,
			type: 'map',
			mapType: map,
			roam: false,

			label: {
				normal: {
					show: true,
					textStyle: {
						//color: 'gray',
						fontSize: 13,
						borderWidth: 1,
					},
					//此处改label
					formatter: function(data) {

						return data.data.value;

					},
				},
				emphasis: {
					show: true,
					textStyle: {
						color: '#4c5262',
						fontSize: 13
					}
				}

			},
			itemStyle: {
				/*normal: {
					borderColor: 'rgba(147, 235, 248, 1)',
					borderWidth: 0.5,
					areaColor:{
						type: 'radial',
						x: 0.5,
						y: 0.5,
						r: 0.8,
						colorStops: [{
							offset: 0,
							color: 'rgba(147, 235, 248, 0)' // 0% 处的颜色
						}, {
							offset: 1,
							color: 'rgba(147, 235, 248, .2)' // 100% 处的颜色
						}],
						globalCoord: false // 缺省为 false
					},
					shadowColor: 'rgba(128, 217, 248, 1)',
					shadowOffsetX: -2,
					shadowOffsetY: 2,
					shadowBlur: 10,
			
				},*/
				emphasis: {
					//areaColor: '#389BB7',
					borderWidth: 1,
					borderColor: '#a5a082',

				}
			},
			data: acdata
		}, ];
		//渲染地图
		chart.setOption(option);

	});

}
//切换行业
function switchIndustry(type, e) {
	$('.hy').each(function() {
		$(this).find('a').removeClass('active')
	})
	$(e).addClass("active")

	industry = type
	//绘制全国地图
	$.getJSON('js/map/map/province/yunnan.json', function(data) {
		var d = [];

		for(var i = 0; i < data.features.length; i++) {
			var name = data.features[i].properties.name
			var id = data.features[i].id
			d.push({
				name: name,
				id: id.substring(0, 4),
			})
		}

		//注册地图
		echarts.registerMap('yunnan', data);
		//绘制地图
		renderMap('yunnan', d, industry, zb);

	});
}

//切换规模
function switchGm(type, e) {
	$('.gm').each(function() {
		$(this).find('a').removeClass('active')
	})
	$(e).addClass("active")

	zb = type
	//绘制全国地图
	$.getJSON('js/map/map/province/yunnan.json', function(data) {
		var d = [];

		for(var i = 0; i < data.features.length; i++) {
			var name = data.features[i].properties.name
			var id = data.features[i].id
			d.push({
				name: name,
				id: id.substring(0, 4),
			})
		}

		//注册地图
		echarts.registerMap('yunnan', data);
		//绘制地图
		renderMap('yunnan', d, industry, zb);

	});
}



//返回
function back(type, e) {

	//绘制全国地图
	$.getJSON('js/map/map/province/yunnan.json', function(data) {
		var d = [];

		for(var i = 0; i < data.features.length; i++) {
			var name = data.features[i].properties.name
			var id = data.features[i].id
			d.push({
				name: name,
				id: id.substring(0, 4),
			})
		}

		//注册地图
		echarts.registerMap('yunnan', data);
		//绘制地图
		renderMap('yunnan', d, industry, zb);

	});
}


function switchTt(type,num){

	$(".mapcontainer").each(function(i,ee){
		if(i==type){
			$(ee).show()
		}else{
			$(ee).hide()
		}
	})
}
